package com.example.midprojcampusspringboot.common;
import com.example.midprojcampusspringboot.domain.entity.TManagerStudent;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 用于excel表格上传分析的
 **/

public class ExcelReader {

    private static final int NAME_COLUMN = 0;
    private static final int FROM_PLACE_COLUMN = 1;
    private static final int PARENT_PHONE_COLUMN = 2;
    private static final int CURRENT_GRADE_COLUMN = 3;
    private static final int IS_FIRST_COLUMN = 4;
    private static final int LOGIN_ID_COLUMN = 5;
    private static final int COMMENT_COLUMN = 6;

    public static List<TManagerStudent> readExcelToStudents(MultipartFile file) throws IOException {
        List<TManagerStudent> students = new ArrayList<>();
        try (InputStream inputStream = file.getInputStream();
             Workbook workbook = new XSSFWorkbook(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() == 0) { // 跳过标题行
                    continue;
                }
                TManagerStudent student = new TManagerStudent();
                student.setStuName(row.getCell(NAME_COLUMN).getStringCellValue());
                student.setFromPlace(row.getCell(FROM_PLACE_COLUMN).getStringCellValue());
                student.setParentPhone(row.getCell(PARENT_PHONE_COLUMN).getStringCellValue());
                student.setCurrentGrade(row.getCell(CURRENT_GRADE_COLUMN).getStringCellValue());
                student.setIsFirst(row.getCell(IS_FIRST_COLUMN).getStringCellValue());
                student.setLoginId((long) row.getCell(LOGIN_ID_COLUMN).getNumericCellValue());
                student.setComment(row.getCell(COMMENT_COLUMN).getStringCellValue());
                students.add(student);
            }
        }
        return students;
    }
}

